<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>提取一行作为对象</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.pg-fetch-assoc.html">pg_fetch_assoc</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.pg-fetch-result.html">pg_fetch_result</a></div>
 <div class="up"><a href="ref.pgsql.html">PostgreSQL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.pg-fetch-object" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">pg_fetch_object</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">pg_fetch_object</span> &mdash; <span class="dc-title">提取一行作为对象</span></p>

   </div>
   <div class="refsect1 unknown-unknown-unknown-unknown-seealss" id="refsect1-function.pg-fetch-object-unknown-unknown-unknown-unknown-seealss">
    <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="type">object</span> <span class="methodname"><strong>pg_fetch_object</strong></span>
       ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$result</code></span>
      [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$row</code></span> 
      [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$result_type</code></span>
     ]] )</div>

    <p class="para rdfs-comment">
     <span class="function"><strong>pg_fetch_object()</strong></span> 返回与所提取行的属性相一致的一个对象。如果出错或者没有更多行可供提取时则返回 <strong><code>FALSE</code></strong>。
    </p>
    <p class="para"> 
     <span class="function"><strong>pg_fetch_object()</strong></span> 和 <span class="function"><a href="function.pg-fetch-array.html" class="function">pg_fetch_array()</a></span> 相似，只有一点区别 － 返回一个对象而不是数组。间接的，这意味着只能通过字段名来访问数据而不能通过偏移量来访问（数字是非法的属性名）。
    </p>
    <p class="para">
     <em><code class="parameter">row</code></em> 是想要取得的行（记录）的编号。第一行为 0。
    </p>
    <p class="para">
     除了速度之外，本函数和 <span class="function"><a href="function.pg-fetch-array.html" class="function">pg_fetch_array()</a></span> 完全一样，而且几乎和 <span class="function"><a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a></span> 一样快（速度上的差别很小）。
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para"> 
      从 4.1.0 版本开始，参数 <em><code class="parameter">row</code></em> 变为可选参数。 
     </p> 
     <p class="para">
      从 4.3.0 开始，<em><code class="parameter">result_type</code></em> 默认值为 PGSQL_ASSOC，而旧版本的默认值是 PGSQL_BOTH。数字属性在这里没有用处，因为在 PHP 中对象的属性不能是数字。
     </p>
     <p class="para">
      <em><code class="parameter">result_type</code></em> 参数在以后的版本中可能会删除。
     </p>
    </p></blockquote>
    <p class="para">
     <div class="example" id="example-2033">
      <p><strong>Example #1 <span class="function"><strong>pg_fetch_object()</strong></span></strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;<br /><br />$database&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"store"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$db_conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"host=localhost&nbsp;port=5432&nbsp;dbname=</span><span style="color: #0000BB">$database</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$db_conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Failed&nbsp;connecting&nbsp;to&nbsp;postgres&nbsp;database&nbsp;</span><span style="color: #0000BB">$database</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #0000BB">$qu&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$db_conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;*&nbsp;FROM&nbsp;books&nbsp;ORDER&nbsp;BY&nbsp;author"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;postgres&nbsp;needs&nbsp;a&nbsp;row&nbsp;counter&nbsp;<br /><br /></span><span style="color: #007700">while&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$qu</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">author&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&nbsp;("</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">year&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"):&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">title&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">++;<br />}<br /><br /></span><span style="color: #0000BB">pg_free_result&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$qu</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">pg_close&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$db_conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      从 4.1.0 开始，<em><code class="parameter">row</code></em> 成为可选参数。每次调用 <span class="function"><strong>pg_fetch_object()</strong></span>，内部的行计数器都会加一。
     </p>
    </p></blockquote>
    <p class="para"> 
     参见 <span class="function"><a href="function.pg-query.html" class="function">pg_query()</a></span>，<span class="function"><a href="function.pg-fetch-array.html" class="function">pg_fetch_array()</a></span>，<span class="function"><a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a></span> 和 <span class="function"><a href="function.pg-fetch-result.html" class="function">pg_fetch_result()</a></span>。
    </p>
   </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.pg-fetch-assoc.html">pg_fetch_assoc</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.pg-fetch-result.html">pg_fetch_result</a></div>
 <div class="up"><a href="ref.pgsql.html">PostgreSQL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
